package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.LhkHotelDetails;
import com.ruoyi.system.domain.LhkHotelService;
import com.ruoyi.system.domain.LhkRoom;
import com.ruoyi.system.mapper.LhkHotelDetailsMapper;
import com.ruoyi.system.mapper.LhkHotelServiceMapper;
import com.ruoyi.system.mapper.LhkRoomMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.LhkHotelInfoMapper;
import com.ruoyi.system.domain.LhkHotelInfo;
import com.ruoyi.system.service.ILhkHotelInfoService;

/**
 * 酒店信息管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-29
 */
@Service
public class LhkHotelInfoServiceImpl implements ILhkHotelInfoService 
{
    @Autowired
    private LhkHotelInfoMapper lhkHotelInfoMapper;

    @Autowired
    private LhkRoomMapper lhkRoomMapper;

    @Autowired
    private LhkHotelDetailsMapper lhkHotelDetailsMapper;

    @Autowired
    private LhkHotelServiceMapper lhkHotelServiceMapper;

    /**
     * 查询酒店信息管理
     * 
     * @param hotelId 酒店信息管理主键
     * @return 酒店信息管理
     */
    @Override
    public LhkHotelInfo selectLhkHotelInfoByHotelId(Long hotelId)
    {
        return lhkHotelInfoMapper.selectLhkHotelInfoByHotelId(hotelId);
    }

    /**
     * 查询酒店信息管理列表
     * 
     * @param lhkHotelInfo 酒店信息管理
     * @return 酒店信息管理
     */
    @Override
    public List<LhkHotelInfo> selectLhkHotelInfoList(LhkHotelInfo lhkHotelInfo)
    {
        List<LhkHotelInfo> lhkHotelInfos = lhkHotelInfoMapper.selectLhkHotelInfoList(lhkHotelInfo);
        for (LhkHotelInfo lhkHotelInfo1 : lhkHotelInfos) {
            Long hotelId = lhkHotelInfo1.getHotelId();
            // 根据hotelId查询空闲的房间
            List<LhkRoom> lhkRooms = lhkRoomMapper.selectAllLhkRoomByHotelId(hotelId);
            int totalRoom = 0;
            int free = 0;
            if (lhkRooms != null){
                totalRoom = lhkRooms.size();   // 房间总数
                if (totalRoom > 0){
                    List<LhkRoom> lhkRooms1 = lhkRoomMapper.selectLhkRoomByHotelId(hotelId);
                    free = lhkRooms1.size();   // 空闲房间数
                }
            }
            lhkHotelInfo1.setTotalRooms((long) totalRoom);
            lhkHotelInfo1.setAvailableRooms((long) free);
            System.out.println("房间总数：" + totalRoom);
            System.out.println("空闲房间数：" + free);
        }
        return lhkHotelInfos;
    }

    // 查询所有酒店信息 --- 微信小程序
    public List<LhkHotelInfo> selectLhkHotelInfoList1(){
        List<LhkHotelInfo> lhkHotelInfos = lhkHotelInfoMapper.selectLhkHotelInfoList1();
        for (LhkHotelInfo lhkHotelInfo1 : lhkHotelInfos) {
            Long hotelId = lhkHotelInfo1.getHotelId();
            Long managerId = lhkHotelInfo1.getManagerId();

            // 根据hotelId查询服务列表
            LhkHotelService lhkHotelService = new LhkHotelService();
            lhkHotelService.setHotelId(hotelId);
            List<LhkHotelService> lhkHotelServices = lhkHotelServiceMapper.selectLhkHotelServiceList(lhkHotelService);
            if (lhkHotelServices!= null){
                lhkHotelInfo1.setLhkServices(lhkHotelServices);
            }

            // 根据hotelId查询酒店详情信息
            LhkHotelDetails lhkHotelDetails = new LhkHotelDetails();
            lhkHotelDetails.setHotelId(hotelId);
            lhkHotelDetails.setManagerId(managerId);
            System.out.println("lhkHotelDetails: " + lhkHotelDetails);
            List<LhkHotelDetails> lhkHotelDetailsList = lhkHotelDetailsMapper.selectLhkHotelDetailsList(lhkHotelDetails);
            System.out.println("lhkHotelDetailsList: " + lhkHotelDetailsList);
            if (lhkHotelDetailsList.size() > 0){
                lhkHotelInfo1.setLhkHotelDetails(lhkHotelDetailsList.get(0));
            }

            // 根据hotelId查询房间信息
            LhkRoom lhkRoom = new LhkRoom();
            lhkRoom.setHotelId(hotelId);
            // 将managerId变为Integer类型
            lhkRoom.setManagerId(managerId.intValue());
            List<LhkRoom> lhkRooms = lhkRoomMapper.selectLhkRoomList(lhkRoom);
            if (lhkRooms != null){
                lhkHotelInfo1.setLhkRooms(lhkRooms);
            }
        }
        return lhkHotelInfos;
    }

    public List<LhkHotelInfo> selectLhkHotelInfoList2(Integer managerId)
    {
        return lhkHotelInfoMapper.selectLhkHotelInfoList2(managerId);
    }

    /**
     * 新增酒店信息管理
     * 
     * @param lhkHotelInfo 酒店信息管理
     * @return 结果
     */
    @Override
    public int insertLhkHotelInfo(LhkHotelInfo lhkHotelInfo)
    {
        lhkHotelInfo.setCreateTime(DateUtils.getNowDate());
        return lhkHotelInfoMapper.insertLhkHotelInfo(lhkHotelInfo);
    }

    /**
     * 修改酒店信息管理
     * 
     * @param lhkHotelInfo 酒店信息管理
     * @return 结果
     */
    @Override
    public int updateLhkHotelInfo(LhkHotelInfo lhkHotelInfo)
    {
        lhkHotelInfo.setUpdateTime(DateUtils.getNowDate());
        return lhkHotelInfoMapper.updateLhkHotelInfo(lhkHotelInfo);
    }

    /**
     * 批量删除酒店信息管理
     * 
     * @param hotelIds 需要删除的酒店信息管理主键
     * @return 结果
     */
    @Override
    public int deleteLhkHotelInfoByHotelIds(Long[] hotelIds)
    {
        return lhkHotelInfoMapper.deleteLhkHotelInfoByHotelIds(hotelIds);
    }

    /**
     * 删除酒店信息管理信息
     * 
     * @param hotelId 酒店信息管理主键
     * @return 结果
     */
    @Override
    public int deleteLhkHotelInfoByHotelId(Long hotelId)
    {
        return lhkHotelInfoMapper.deleteLhkHotelInfoByHotelId(hotelId);
    }
}
